package com.test.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.R;
import com.test.entity.Group;
import com.test.entity.Item;

import java.util.ArrayList;

/**
 * 折叠样式adapter加载
 *
 * @Author Musique
 * 2020/8/6
 */
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    private ArrayList<Group> groupList;
    private ArrayList<ArrayList<Item>> icon;
    private Context mContext;


    //静态代码块(组)
    static class GroupHolder {
        TextView groupName;
    }

    //静态代码块(组)
    public static class ItemHolder {
        ImageView aIcon;
        TextView title;
    }

    public ExpandableListViewAdapter(ArrayList<Group> groupList, ArrayList<ArrayList<Item>> icon, Context mContext) {
        this.groupList = groupList;
        this.icon = icon;
        this.mContext = mContext;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return icon.size();
    }

    @Override
    public Group getGroup(int i) {
        return groupList.get(i);
    }

    @Override
    public Item getChild(int i, int i1) {
        return icon.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    /**
     * 取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象
     */
    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        GroupHolder groupHolder = new GroupHolder();
        if (view == null) {
            // 获取定义资源格式
            view = LayoutInflater.from(mContext).inflate(R.layout.item_list_demo, viewGroup, false);
            // 查找对应id
            groupHolder.groupName = view.findViewById(R.id.tv_item_list);
            view.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) view.getTag();
        }
        groupHolder.groupName.setText(groupList.get(i).getGroupName());
        return view;
    }

    /**
     * 取得显示给定分组给定子位置的数据用的视图
     */
    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

        ItemHolder itemHolder = new ItemHolder();
        if (view == null) {
            // 获取定义资源格式
            view = LayoutInflater.from(mContext).inflate(R.layout.item_list_demo_child, viewGroup, false);
            // 查找对应id
            itemHolder.aIcon = view.findViewById(R.id.img_icon);
            itemHolder.title = view.findViewById(R.id.tv_title);
            view.setTag(itemHolder);
        } else {
            itemHolder = (ItemHolder) view.getTag();
        }
        itemHolder.aIcon.setBackgroundResource(icon.get(i).get(i1).getAIcon());
        itemHolder.title.setText(icon.get(i).get(i1).getTitle());
        return view;
    }

    /**
     * 设置子列表是否可选中
     */
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

}
